package br.com.janalise.util;

import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

public class Arquivo {

    public static File gravarArquivo(String conteudo, String path)
            throws FileNotFoundException, UnsupportedEncodingException {
        //String str = noSpace(conteudo);
        File file = new File(path);
        PrintStream saida = new PrintStream(new FileOutputStream(file),true,"UTF-8");
        saida.print(conteudo);
        return file;
    }

    public static String noSpace(String str) {
        return str.replace("  ", "").replace("\n", "").trim();
    }

    public static String lerArquivo(String path) throws IOException {
        BufferedReader in = new BufferedReader(new FileReader(path));
        StringBuffer str = new StringBuffer();
        while (in.ready()) {
            str.append(in.readLine() + "\n");
        }
        in.close();
        return str.toString();
    }

    public static File criarPastas(String nomePasta) {
        String path = System.getProperty("user.home") + "/" + nomePasta;
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }

        return file;
    }

    public static void abrir(File file) throws IOException {
        Desktop desktop = Desktop.getDesktop();
        desktop.open(file);
    }
}
